
#include "stdafx.h"

TEST(selectTest, select_in_inout)
{
    int nfds = 11;
    fd_set fdset;
    fdset.fd_count = 3;
    fdset.fd_array[0] = 1;
    fdset.fd_array[1] = 2;
    fdset.fd_array[2] = 3;
    struct timeval timevl;
    timevl.tv_sec = 10000;
    timevl.tv_usec = 10001;
    
    Header header(GetCurrentThreadId(), "select");
    select_in_inout selectIN(nfds, &fdset, &fdset, &fdset, &timevl);
    select_in_inout selectOUT;

    std::vector<char> buffer;
    Serialize(header, selectIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(selectOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (selectIN.nfds_, selectOUT.nfds_);
    if (selectIN.readfds_ && selectOUT.readfds_)
    {
        EXPECT_EQ (selectIN.readfds_->fd_count, selectOUT.readfds_->fd_count);
        EXPECT_TRUE ( !memcmp(selectIN.readfds_->fd_array, 
                              selectOUT.readfds_->fd_array,
                              sizeof(SOCKET)*selectIN.readfds_->fd_count));
    }
    else {
        EXPECT_FALSE(selectIN.readfds_);
        EXPECT_FALSE(selectOUT.readfds_);
    }
    if (selectIN.writefds_ && selectOUT.writefds_)
    {
        EXPECT_EQ (selectIN.writefds_->fd_count, selectOUT.writefds_->fd_count);
        EXPECT_TRUE ( !memcmp(selectIN.writefds_->fd_array, 
                              selectOUT.writefds_->fd_array,
                              sizeof(SOCKET)*selectIN.writefds_->fd_count));
    }
    else {
        EXPECT_FALSE(selectIN.writefds_);
        EXPECT_FALSE(selectOUT.writefds_);
    }
    if (selectIN.exceptfds_ && selectOUT.exceptfds_)
    {
        EXPECT_EQ (selectIN.exceptfds_->fd_count, selectOUT.exceptfds_->fd_count);
        EXPECT_TRUE ( !memcmp(selectIN.exceptfds_->fd_array, 
                              selectOUT.exceptfds_->fd_array,
                              sizeof(SOCKET)*selectIN.exceptfds_->fd_count));
    }
    else {
        EXPECT_FALSE(selectIN.exceptfds_);
        EXPECT_FALSE(selectOUT.exceptfds_);
    }
    if (selectIN.timeout_ && selectOUT.timeout_)
    {
        EXPECT_EQ (selectIN.timeout_->tv_sec, selectOUT.timeout_->tv_sec);
        EXPECT_EQ (selectIN.timeout_->tv_usec, selectOUT.timeout_->tv_usec);
    }
    else {
        EXPECT_FALSE(selectIN.timeout_);
        EXPECT_FALSE(selectOUT.timeout_);
    }
}

TEST(selectTest, select_in_inout_NULL_parameters)
{
    Header header(GetCurrentThreadId(), "select");
    select_in_inout selectIN(11, NULL, NULL, NULL, NULL);
    select_in_inout selectOUT;

    std::vector<char> buffer;
    Serialize(header, selectIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(selectOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (selectIN.nfds_, selectOUT.nfds_);
    if (selectIN.readfds_ && selectOUT.readfds_)
    {
        EXPECT_EQ (selectIN.readfds_->fd_count, selectOUT.readfds_->fd_count);
        EXPECT_TRUE ( !memcmp(selectIN.readfds_->fd_array, 
                              selectOUT.readfds_->fd_array,
                              sizeof(SOCKET)*selectIN.readfds_->fd_count));
    }
    else {
        EXPECT_FALSE(selectIN.readfds_);
        EXPECT_FALSE(selectOUT.readfds_);
    }
    if (selectIN.writefds_ && selectOUT.writefds_)
    {
        EXPECT_EQ (selectIN.writefds_->fd_count, selectOUT.writefds_->fd_count);
        EXPECT_TRUE ( !memcmp(selectIN.writefds_->fd_array, 
                              selectOUT.writefds_->fd_array,
                              sizeof(SOCKET)*selectIN.writefds_->fd_count));
    }
    else {
        EXPECT_FALSE(selectIN.writefds_);
        EXPECT_FALSE(selectOUT.writefds_);
    }
    if (selectIN.exceptfds_ && selectOUT.exceptfds_)
    {
        EXPECT_EQ (selectIN.exceptfds_->fd_count, selectOUT.exceptfds_->fd_count);
        EXPECT_TRUE ( !memcmp(selectIN.exceptfds_->fd_array, 
                              selectOUT.exceptfds_->fd_array,
                              sizeof(SOCKET)*selectIN.exceptfds_->fd_count));
    }
    else {
        EXPECT_FALSE(selectIN.exceptfds_);
        EXPECT_FALSE(selectOUT.exceptfds_);
    }
    if (selectIN.timeout_ && selectOUT.timeout_)
    {
        EXPECT_EQ (selectIN.timeout_->tv_sec, selectOUT.timeout_->tv_sec);
        EXPECT_EQ (selectIN.timeout_->tv_usec, selectOUT.timeout_->tv_usec);
    }
    else {
        EXPECT_FALSE(selectIN.timeout_);
        EXPECT_FALSE(selectOUT.timeout_);
    }
}

TEST(selectTest, select_out_inout)
{
    fd_set fdset;
    fdset.fd_count = 3;
    fdset.fd_array[0] = 1;
    fdset.fd_array[1] = 2;
    fdset.fd_array[2] = 3;
    
    Header header(GetCurrentThreadId(), "select");
    select_out_inout selectIN(11, &fdset, &fdset, &fdset, 7);
    select_out_inout selectOUT;

    std::vector<char> buffer;
    Serialize(header, selectIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(selectOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    if (selectIN.readfds_ && selectOUT.readfds_)
    {
        EXPECT_EQ (selectIN.readfds_->fd_count, selectOUT.readfds_->fd_count);
        EXPECT_TRUE ( !memcmp(selectIN.readfds_->fd_array, 
                              selectOUT.readfds_->fd_array,
                              sizeof(SOCKET)*selectIN.readfds_->fd_count));
    }
    else {
        EXPECT_FALSE(selectIN.readfds_);
        EXPECT_FALSE(selectOUT.readfds_);
    }
    if (selectIN.writefds_ && selectOUT.writefds_)
    {
        EXPECT_EQ (selectIN.writefds_->fd_count, selectOUT.writefds_->fd_count);
        EXPECT_TRUE ( !memcmp(selectIN.writefds_->fd_array, 
                              selectOUT.writefds_->fd_array,
                              sizeof(SOCKET)*selectIN.writefds_->fd_count));
    }
    else {
        EXPECT_FALSE(selectIN.writefds_);
        EXPECT_FALSE(selectOUT.writefds_);
    }
    if (selectIN.exceptfds_ && selectOUT.exceptfds_)
    {
        EXPECT_EQ (selectIN.exceptfds_->fd_count, selectOUT.exceptfds_->fd_count);
        EXPECT_TRUE ( !memcmp(selectIN.exceptfds_->fd_array, 
                              selectOUT.exceptfds_->fd_array,
                              sizeof(SOCKET)*selectIN.exceptfds_->fd_count));
    }
    else {
        EXPECT_FALSE(selectIN.exceptfds_);
        EXPECT_FALSE(selectOUT.exceptfds_);
    }
}

//TEST(selectTest, select_out_inout_NULL_parameters)
//{
//    
//}
